package com.example.liwushuo.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import com.alibaba.fastjson.JSONObject;
import com.example.liwushuo.BaseApp;
import com.example.liwushuo.GJActivity;
import com.example.liwushuo.HomeListItemActivity;
import com.example.liwushuo.LittleGoodsItem;
import com.example.liwushuo.LoginActivity;
import com.example.liwushuo.R;
import com.example.liwushuo.adapter.GridAdapter;
import com.example.liwushuo.adapter.ImageAdapter;
import com.example.liwushuo.adapter.SelectionAdapter;
import com.example.liwushuo.bean.Banner;
import com.example.liwushuo.bean.BaseHome;
import com.example.liwushuo.bean.BaseViewPage;
import com.example.liwushuo.bean.HomeGridBase;
import com.example.liwushuo.bean.HomeItem;
import com.example.liwushuo.bean.Promotion;
import com.example.liwushuo.urls.urls;
import com.example.liwushuo.utils.Utils;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.Toast;

/**
 * @author 时伟超 精选的界面
 */
public class Selection extends Fragment implements
		OnRefreshListener2<ListView>, OnItemClickListener {

	private static final String PullToRefreshListView = null;
	private PullToRefreshListView PulllistView;
	private List<HomeItem> list;
	private SelectionAdapter adapter;
	private int id;
	private String url;

	// -------------------------GridView----------------------------------

	private GridView gridView;
	private List<Promotion> GridList;
	private GridAdapter GAdapter;
	private String GridUrl;

	// --------------------------ViewPager-------------------------------------------

	private ViewPager handViewPager;
	private ImageAdapter imgAdapter;
	private List<ImageView> imgs;
	private List<Banner> banners;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Toast.makeText(getActivity(), "" + getArguments().getInt("id"), 1)
				.show();
		id = getArguments().getInt("id");

		url = urls.getHomeUrl(id, Utils.getParams(getActivity(), "gender"),
				Utils.getParams(getActivity(), "generation"));
		GridUrl = urls.getGridViewUrl(Utils.getParams(getActivity(), "gender"),
				Utils.getParams(getActivity(), "generation"));

		View view = inflater.inflate(R.layout.fragment_selection, null);

		initView(view, inflater);
		initData();

		return view;
	}

	private void initView(View view, LayoutInflater inflater) {
		PulllistView = (PullToRefreshListView) view
				.findViewById(R.id.selection_listView);
		list = new ArrayList<HomeItem>();
		adapter = new SelectionAdapter(getActivity(), list);
		// 滚动式不加载数据
		PulllistView.setOnScrollListener(new PauseOnScrollListener(BaseApp
				.getBitmapUtils(), true, false));
		AddHandViewPager(PulllistView, inflater);
		AddHandView(PulllistView, inflater);
		PulllistView.setAdapter(adapter);
		PulllistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getActivity(),""+list.get(position).getId() , 1).show();
				long id2 = list.get(position).getId();
				Intent intent = new Intent(getActivity(),
						HomeListItemActivity.class);
				intent.putExtra("id", id2);
				startActivity(intent);
			}
		});
	}

	// 添加viewPagerr
	private void AddHandViewPager(PullToRefreshListView pulllistView,
			LayoutInflater inflater) {
		View view = inflater.inflate(R.layout.home_selection_viewpager, null);
		banners = new ArrayList<Banner>();
		handViewPager = (ViewPager) view
				.findViewById(R.id.home_selection_welcome_viewPager);
		imgs = new ArrayList<ImageView>();
		imgAdapter = new ImageAdapter(imgs);
		handViewPager.setAdapter(imgAdapter);
		// 下载滚动栏的图片
		loadViewPagerData();
		pulllistView.getRefreshableView().addHeaderView(view);
	}

	private void AddHandView(PullToRefreshListView PulllistView,
			LayoutInflater inflater) {

		ListView listView = PulllistView.getRefreshableView();

		View view = inflater.inflate(R.layout.gridview, null);
		gridView = (GridView) view.findViewById(R.id.gridView_selection);
		GridList = new ArrayList<Promotion>();
		GAdapter = new GridAdapter(getActivity(), GridList);
		gridView.setOnItemClickListener(this);
		gridView.setAdapter(GAdapter);
		initGridViewData();
		listView.addHeaderView(view);
	}

	// 下载GridView的数据

	private void initGridViewData() {

		BaseApp.getHttpUtils().send(HttpMethod.GET, GridUrl,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {

					}

					@Override
					public void onSuccess(ResponseInfo<String> response) {

						String json = response.result;

						HomeGridBase gridBase = JSONObject.parseObject(json,
								HomeGridBase.class);

						if (gridBase != null
								&& "OK".equals(gridBase.getMessage())) {
							GridList.addAll(gridBase.getData().getPromotions());

							// Log.i("gRIDlIST", "------"+GridList.size());
							GAdapter.notifyDataSetChanged();
						}
					}
				});
	}

	// 下载listView的数据
	private void initData() {

		BaseApp.getHttpUtils().send(HttpMethod.GET, url,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						Toast.makeText(getActivity(), "连接网络失败", 1).show();

					}

					@Override
					public void onSuccess(ResponseInfo<String> response) {

						String json = response.result;
						// Log.i("jsom", "-----------" + json);

						BaseHome baseHome = JSONObject.parseObject(json,
								BaseHome.class);
						if ("OK".equals(baseHome.getMessage())) {
							list.addAll(baseHome.getData().getItems());
							adapter.notifyDataSetChanged();
						}
					}
				});

	}

	// 下载头部滚动条的数据
	private void loadViewPagerData() {
		BaseApp.getHttpUtils().send(HttpMethod.GET, urls.ViewPagerUrl,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {

					}

					@Override
					public void onSuccess(ResponseInfo<String> response) {
						String json = response.result;
						BaseViewPage baseViewPage = JSONObject.parseObject(
								json, BaseViewPage.class);
						if (baseViewPage != null
								&& "OK".equals(baseViewPage.getMessage())) {
							banners.addAll(baseViewPage.getData().getBanners());
							// 下载图片
							loadImage();
						}
					}
				});
	}

	private void loadImage() {

		ImageView img = null;
		for (int i = 0; i < banners.size(); i++) {

			img = new ImageView(getActivity());
			img.setScaleType(ScaleType.FIT_XY);
			ImageLoader.getInstance().displayImage(
					banners.get(i).getImage_url(), img);
			imgs.add(img);
		}
		imgAdapter.notifyDataSetChanged();
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = null;
		switch (GridList.get(position).getId()) {
		case 1:
		case 2:
			intent = new Intent(getActivity(), LittleGoodsItem.class);
			intent.putExtra("id", GridList.get(position).getId());
			break;
		case 3:
			// 先判断是否登录没有登录就跳转到登录页面
			intent = new Intent(getActivity(), LoginActivity.class);
			break;
		case 4:
			intent = new Intent(getActivity(), GJActivity.class);
			break;
		}
		startActivity(intent);

	}
}
